﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gunningine
{
    using System.Drawing;
    using System.Drawing.Imaging;

    internal class ImageLoader : IResourceLoader
    {
        #region IResourceLoader implementation.

        public string Name
        {
            get { return "Image"; }
        }

        public string[] SupportedFormats
        {
            get { return new string[] { ".png", ".jpg", ".tga", ".bmp" }; }
        }

        /// <summary>
        /// Load image into memory.
        /// </summary>
        public void Load(Resource res)
        {
            ImageResource imageResource = (ImageResource)res;

            try
            {
                imageResource.Data = new Bitmap(imageResource.Filename);
            }
            catch (Exception)
            {
                throw new ResourceInvalidException(imageResource.Filename);
            }


            if (imageResource.Data.PixelFormat == PixelFormat.Format24bppRgb)
                imageResource.Channels = 3;
            else if (imageResource.Data.PixelFormat == PixelFormat.Format32bppArgb)
                imageResource.Channels = 4;
            else
                throw new Exception("Image '" + imageResource.Filename + "' is not a supported format!");

            imageResource.Width = imageResource.Data.Width;
            imageResource.Height = imageResource.Data.Height;
        }

        #endregion
    }
}
